//
//  BaseSendData.m
//  BalanceCar
//
//  Created by 郑信鸿 on 2017/8/24.
//  Copyright © 2017年 郑信鸿. All rights reserved.
//

#import "BaseSendData.h"
#import "ByteTool.h"

@interface BaseSendData ()




@end

@implementation BaseSendData

+ (BaseSendData *)shareManager
{
    static BaseSendData *managerInstance = nil;
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        managerInstance = [[self alloc] init];
    });
    return managerInstance;
}

- (instancetype)init{
    
    if (self = [super init]) {

    }
    return self;
}

- (void)sendDefaultData{
    
    Byte connectPw[12] = { 0xA5, 0x0a, 0x01,0x00,0x00,0x00,0x00,0x00,0x00};
    
    connectPw[9] = [self getCheckCodeWithbytes:connectPw count:9];
    
    NSData *pwdata = [NSData dataWithBytes:connectPw length:12];
    
    [[BalanceBuleManager shareManager] peripheralWriteData:pwdata];
    
}

//链接密码
- (NSData *)sendConnectPw:(Byte[])pw complete:(CompleteBlock)complete{
    
    self.pwComplete = complete;
    
    Byte connectPw[10] = { 0xA5, 0x0a, 0x01};
    
    for (int i = 3; i < 9; i++) {
        
        connectPw[i] = pw[i - 3];
        
    }
    
    connectPw[9] = [self getCheckCodeWithbytes:connectPw count:9];
    
    NSData *pwdata = [NSData dataWithBytes:connectPw length:10];
    
    [[BalanceBuleManager shareManager] peripheralWriteData:pwdata];
    
    return pwdata;
    
}


- (NSData *)sendLockCarWithByte:(Byte)lock complete:(CompleteBlock)complete{
     self.lockComplete = complete;
    Byte lockByte[7] = {0xA5, 0x05, 0x02, lock};
    
    lockByte[4] = [self getCheckCodeWithbytes:lockByte count:4];
    
    NSData *lockData = [NSData dataWithBytes:lockByte length:7];
     [[BalanceBuleManager shareManager] peripheralWriteData:lockData];
    return lockData;
}

+ (NSData *)sendTravelParamsOrder{
    
    Byte travelByte[6] = {0xA5, 0x04, 0x03, 0xAC};
    
    NSData *travelData = [NSData dataWithBytes:travelByte length:6];
    
    [[BalanceBuleManager shareManager] peripheralWriteData:travelData];
    
    return travelData;
    
    
}

- (NSData *)sendCarLightWithByte:(Byte)lightState complete:(CompleteBlock)complete{
     self.lightComplete = complete;
    Byte lightByte[7] = {0xA5, 0x05, 0x04,lightState};
    lightByte[4] = [self getCheckCodeWithbytes:lightByte count:4];
    NSData *lightData = [NSData dataWithBytes:lightByte length:7];
     [[BalanceBuleManager shareManager] peripheralWriteData:lightData];

    return lightData;
}

- (NSData *)sendGearsWithByte:(Byte)gears complete:(CompleteBlock)complete{
    
     self.greesComplete = complete;
    
    Byte gearsByte[7] = {0xA5, 0x05, 0x05,gears};
    
    gearsByte[4] = [self getCheckCodeWithbytes:gearsByte count:4];
    
    NSData *gearsData = [NSData dataWithBytes:gearsByte length:7];
    
     [[BalanceBuleManager shareManager] peripheralWriteData:gearsData];
    
    return gearsData;
    
}

- (NSData *)sendIsAssistance:(BOOL)isAssistance complete:(CompleteBlock)complete{
    
     self.assisComplete = complete;
    
    Byte assitance[7] = {0xA5, 0x05, 0x07};
    if (isAssistance) {
        assitance[3] = 0x01;
    }else{
        assitance[3] = 0x00;
    }
    assitance[4] = [self getCheckCodeWithbytes:assitance count:4];
    
    NSData *assitanceData = [NSData dataWithBytes:assitance length:7];
     [[BalanceBuleManager shareManager] peripheralWriteData:assitanceData];
    return assitanceData;
    
}

- (NSData *)sendIsNavigate:(BOOL)isNavigate complete:(CompleteBlock)complete{
    
     self.navComplete = complete;
    
    Byte navigate[7] = {0xA5, 0x05, 0x08};
    if (isNavigate) {
        navigate[3] = 0x01;
    }else{
        navigate[3] = 0x00;
    }
    navigate[4] = [self getCheckCodeWithbytes:navigate count:4];
    
    NSData *navigateData = [NSData dataWithBytes:navigate length:7];
     [[BalanceBuleManager shareManager] peripheralWriteData:navigateData];
    return navigateData;
    
}


- (Byte)getCheckCodeWithbytes:(Byte[])bytes count:(int)count{
    
    Byte tempByte = 0x00;
    
    for (int i = 0; i < count; i++) {
        
        Byte byte = bytes[i];
        
        tempByte = tempByte + byte;
    }
    //去最后两位
    tempByte = tempByte % 0x100;
    return tempByte;
}

+ (NSData *)sendConnectPw:(Byte[])pw{
    
    [BaseSendData shareManager].pwComplete = nil;
    
    Byte connectPw[10] = { 0xA5, 0x0a, 0x01};
    
    for (int i = 3; i < 9; i++) {
        
        connectPw[i] = pw[i - 3];
        
    }
    
    connectPw[9] = [[BaseSendData shareManager] getCheckCodeWithbytes:connectPw count:9];
    
    NSData *pwdata = [NSData dataWithBytes:connectPw length:10];
    
    [[BalanceBuleManager shareManager] peripheralWriteData:pwdata];
    
    return pwdata;
}

+ (NSData *)sendChangePasswordWithByte:(Byte[])password{
    
    Byte pwByte[16] = {0xA5, 0x10, 0x06};
    for (int i = 3; i < 15; i++) {
        pwByte[i] = password[i - 3];
    }
    pwByte[15] = [[BaseSendData shareManager] getCheckCodeWithbytes:pwByte count:15];
    NSData *pwData = [NSData dataWithBytes:pwByte length:16];
    [[BalanceBuleManager shareManager] peripheralWriteData:pwData];
    return pwData;
}

@end
